﻿using System;
using System.IO;
using System.Reflection;
using System.Text;

namespace MudEngine
{
	public static class Util
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="filename"></param>
		/// <returns></returns>
		public static bool EnsureDirectoryExists(string filename, bool emptyDirectory)
		{
			FileInfo info = new FileInfo(filename);
			string path = info.DirectoryName;

			if (Directory.Exists(path) && emptyDirectory)
			{
				try
				{
					Directory.Delete(path, true);
				}
				catch (IOException ioe)
				{
					Log.Info(typeof(Util), "unable to delete folder " + path, ioe);
				}
			}

			if (!Directory.Exists(path))
			{
				try
				{
					Directory.CreateDirectory(path);
				}
				catch (Exception e)
				{
					Log.Fatal(typeof(Util), "Unable to create folder structure.", e);
					return false;
				}
			}
			return Directory.Exists(path);
		}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string SafePath(string path)
        {
            if (String.IsNullOrWhiteSpace(path))
                return "";

            if (path[0] != Path.DirectorySeparatorChar)
                return Path.DirectorySeparatorChar + path;
            return path;
        }
	}
}
